﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Linq;

using Tools.Json;

using SY.Uap.Entity;
using SY.WebSite.Entity;
using SY.WebSite.BLL;

namespace SY.WebSite.Web.Handler
{
    public class IntegrationHandler : InternalWebHandler, IHttpHandler
    {
        public new void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            switch (Action)
            {
                case "query":
                    string method = RequestStringParam("method");
                    if (!string.IsNullOrEmpty(method))
                        QueryMore(method);
                    else
                        Query();
                    break;
                case "transfer":
                    Transfer();
                    break;
                default:
                    break;
            }

            context.Response.End();
        }

        public void Transfer()
        {
            int userId = Tools.Web.Request.GetFormInt("toUserId", 0);
            int amount = Tools.Web.Request.GetFormInt("amount", 0);
            string reason = Tools.Web.Request.GetFormString("reason");
            int type = Tools.Web.Request.GetFormInt("type", 3);

            User toUser = BizUser.GetByKey(userId);
            if (toUser == null || toUser.UserId == CurrentUser.UserId)
                BreakResponse();

            if (amount <= 0 || string.IsNullOrEmpty(reason))
                BreakResponse();

            if (BizAuctionItemBid.CheckUserHaveBidding(CurrentUser.UserId))
            {
                BreakResponse("当前存在尚未结算的拍卖纪录，暂时不能交易素币");
            }

            bool result = BizIntegrationTransaction.Transaction(CurrentUser, toUser, amount, type, Tools.StringUtil.HtmlEncode(reason));
            if (!result)
                BreakResponse("交易的素币超过您的可用素币，请确认后重新操作");
            
            Response.Write(new JsonTransfer().Add("success", "success"));
        }

        public void QueryMore(string method)
        {
            int pageIndex = RequestIntParam("pageIndex");
            int pageSize = RequestIntParam("pageSize", 20);
            JsonTransfer jt = new JsonTransfer();
            IEnumerable<Hashtable> list = null;
            switch (method)
            {
                case "available":
                    list = BizIntegration.GetAvailable(pageIndex, pageSize);
                    break;
                case "used":
                    list = BizIntegration.GetUsed(pageIndex, pageSize);
                    break;
            }
            jt.Add("list", list);
            Response.Write(jt);
        }

        public void Query()
        {
            int userId = RequestIntParam("userId");
            if (userId <= 0)
                userId = CurrentUser.UserId;

            int pageIndex = RequestIntParam("pageIndex");
            int pageSize = RequestIntParam("pageSize", 20);

            string start = RequestStringParam("start");
            string end = RequestStringParam("end");

            int type = RequestIntParam("type");//类型：0全部 1收入 2支出

            User user = BizUser.GetByKey(userId);
            if (user == null)
                BreakResponse();

            IEnumerable<IntegrationTransaction> list = type == 0 ? BizIntegrationTransaction.GetListByUserId(userId) : BizIntegrationTransaction.GetListByUserIdAndType(userId, type);

            if(!string.IsNullOrEmpty(start))
            {
                DateTime starDate;
                if(DateTime.TryParse(start, out starDate))
                {
                    list = list.Where(l => l.DateTime >= starDate);
                }
                else
                {
                    BreakResponse();
                }
            }

            if (!string.IsNullOrEmpty(end))
            {
                DateTime endDate;
                if (DateTime.TryParse(end, out endDate))
                {
                    list = list.Where(l => l.DateTime <= endDate.AddDays(1));
                }
                else
                {
                    BreakResponse();
                }
            }

            JsonTransfer jt = new JsonTransfer();
            jt.Add("count", list.Count());
            jt.Add("list", list.Skip(pageSize * pageIndex).Take(pageSize));
            jt.Add("jobName", user.JobName);
            jt.Add("integration", BizIntegration.GetByKey(userId));
            Response.Write(jt);
        }
    }
}